package com.codefish.codefish_blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codefish.codefish_blog.config.property.BlogWebProperties;
import com.codefish.codefish_blog.entity.Article;
import com.codefish.codefish_blog.entity.ArticleSnapshot;
import com.codefish.codefish_blog.entity.CommentRecord;
import com.codefish.codefish_blog.entity.User;
import com.codefish.codefish_blog.redis.RedisArticleService;
import com.codefish.codefish_blog.service.ArticleLikeRecordService;
import com.codefish.codefish_blog.service.ArticleService;
import com.codefish.codefish_blog.service.CommentLikeRecordService;
import com.codefish.codefish_blog.service.CommentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

/**
 * 文章内容相关controller类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/03/22 下午 10:39
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleLikeRecordService articleLikeRecordService;
    @Autowired
    CommentRecordService commentRecordService;
    @Autowired
    CommentLikeRecordService commentLikeRecordService;
    @Autowired
    RedisArticleService redisArticleService;


    /**
     * 获取文章内容
     *
     * @param articleId 文章id
     * @param model     model对象
     * @param session   session对象
     * @return 文章内容视图
     */
    @GetMapping("/{articleId}")
    public String getArticle(@PathVariable("articleId") Long articleId, Model model, HttpSession session) {
        //获取文章内容
        Article article = articleService.getById(articleId);
        //redis中文章访问数加一
        long watchCount = redisArticleService.increaseWatchCount(articleId);
        article.setWatched(watchCount);
        //文章信息存入model中
        model.addAttribute("article", article);
        //查看用户登录状态
        User user = (User) session.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        model.addAttribute("isLogin", user != null);
        //若用户登录，1.查询对该文章是否点赞;
        // 2.查询对该文章下的评论的点赞情况
        if (user != null) {
            //文章是否点赞
            boolean isLike = articleLikeRecordService.isLike(articleId, user.getId());
            model.addAttribute("isLike", isLike);
            //文章评论的点赞情况
            Set<Long> commentLikeSet = commentLikeRecordService.searchLikesOnComments(articleId, user.getId());
            model.addAttribute("commentLikeSet", commentLikeSet);
        }
        //解析文章标签
        String[] tags = article.getArticleTags().split(",");
        model.addAttribute("articleTags", tags);
        //加载文章评论(一级和二级文章评论)
        List<List<CommentRecord>> commentRecordList = commentRecordService.loadArticleComments(articleId);
        model.addAttribute("commentRecordList", commentRecordList);
        return "blog-details";
    }
}
